Working with Windows Containers and Docker: Up and Running

Comments 0

Share to social media

Overview

In the previous article in this series, Working with Windows Containers and Docker: The Basics, I examined the basics of how container virtualization is being implemented in the Windows Server 2016 operating system. With this basic run-through of Windows Containers out of the way, I can go on to explain how you can work with this new feature in Windows Server 2016.

Introduction

In Part 1 of this series, we learned about Windows Containers Fundamentals and Docker. Containers provide lightweight and Agile virtualization capabilities that developers can use to quickly deploy and update apps in their own development environment. This article will explain how to create and manage Windows Server Containers using Docker. By the end of this step, we will have our Container Host up and running with just one IIS container. That’s pretty cool!

Windows Server 2016 with Containers on Azure

For many Developers and Sysadmins, it makes a lot of sense to deploy services to the Microsoft Azure cloud, but you have the additional option of deploying your Container Host to the Microsoft Azure cloud. One of the advantages of using Azure in this way is that you can deploy a Windows Server 2016 virtual machine with the Containers feature already installed. By doing so, you are then able to experiment with Windows Server Container, with both Server Core and Nano Server Container OS Images installed and ready to use with Docker.

Log in to your Azure portal and create a virtual machine using the Windows Server 2016 Core with Containers image, as shown below:

Docker Commands

How do you set about working with Docker on Windows Server? Before going more into the practical side, we need to get familiar with the essential Docker PowerShell commands. You can run the docker –help command:

There is enough here for everyday use. We’ll get started by using one of the most common of the Docker commands:

This command shows you which Docker Images are available locally. After installing Windows Containers no Docker Images are, as yet, installed. The first thing you have to do is to pull Windows Server Images from the Docker Hub. You don’t need a Docker Hub Account to perform this task:

I will describe this command in more detail in the next section. When you have at least one image available, you can start a Windows Container:

It means that a Windows Container called <Container_Name> will be started from the Image called <Image_Name>. When you have more than one container running, it can be useful to get a list of all running containers:

Finally, you will need some information about your containers, such as the IP Address. Use the following to retrieve this information:

Retrieve Images from Docker Hub

In Docker container parlance, the image is the template from which you spawn new containers. We can download some pre-built Docker images from the Docker Hub. At the time this was written, Microsoft has two container images in their gallery: Nano Server and Windows Server Core. Let’s download them both by pulling the images from Docker Hub:

Ok, but how to know the exact name of the image that you need? That’s pretty simple. On Docker Hub, you will notice the following section named “Docker Pull Command” as shown below.

The only necessary step is to copy and paste the command to download the image. Once the image installation process completes (it can take a while, depending on your Internet connection speed), confirm that Windows Server Core image is available locally. Run Docker images to check:

If you have deployed the pre-configured virtual machine in Azure, the two base images are already pulled.

Deploy a new Windows Container

Now we will use the Docker run command to deploy a new container named CoreServerCMD that uses the Windows Server Core image. The -it switch denotes an interactive session, and cmd.exe means that we want to enter the container inside a new cmd.exe console. Be careful, the repository name must be lowercase.

Basically, the docker run translates the cmd.exe command within the new Server Core-based container. Now, we have a container named CoreServerCMD which is running. We can check with the Docker ps command:

The Docker ps command by default shows only running containers but passing the -a flag to the command will show all containers. You can start and stop containers very simply by running:

When you start a Windows Container, take a look at the Task Manager tool and you will notice a new process called “Docker”. When you stop your container, this process will disappear:

OK, the container is up and running but you don’t need to execute the docker run command once again. To re-enter the running container, the docker attach command will do the job:

Containers have both a name and identifier, and either of them can be used to identify the container to inspect. Inspect is a core Docker instruction (Docker Docs- Docker Inspect) that allows you to get all the information about Container and Image. Below, I inspect my container:

The output has been truncated. You can display specific settings using the –f parameter:

Build your own container image

Docker containers are created by using base images. Basically, an image can contain nothing more than the operating-system fundamentals, or it can contain a full application stack ready for use. So, let’s see an example. You may have a custom website that you want to deploy in a Windows Container. You have two ways to do the job. You can run a Windows Container or just use a Docker file. To run a Windows Container, install and configure the IIS role and finally import your html files: To use a Docker File takes a bit more explaining. Just imagine, for the sake of this example, that you have a folder called “MyWebSite” containing two files:

  • Index.html
  • Dockerfile

What is a Dockerfile?

Dockerfile is a script, composed of various “commands” also called “instructions” and arguments listed successively to automatically perform actions on a base image in order to create a new one. The Dockerfile greatly helps with deployments by automating and simplifying this process.

Below is the content of my Dockerfile:

My Dockerfile begins with a “FROM” keyword. This keyword defines an image from which the build process starts. In my example, I will use the Microsoft IIS image, then create a folder and finally run some PowerShell commands via the “RUN” keyword. Thanks to this Dockerfile, Docker will create a new container image. A container image is the captured state information of a container that you have created and customized on a container host. Once you have customized a container image for a particular purpose, you can then use it to create new containers from it. It’s something like a “Template” for creating new containers for some specific purpose that you can list with the Docker images command.

Creating a container image

OK, my Dockerfile is ready. Here are the commands I need to run to create my new image:

“myiis” is the repository name and must be lowercase. At the end of my Dockerfile, I expose my website on the 8080 port number. At this step, I can confirm that my container image has been successfully created:

Now, I just have to use Docker run to create a new container from my new container image:

That’s cool, my container is up and running. I can get the container IP Address with the following command and go to my website on the following URL: http://172.21.158.179:8080

Please be careful; because if you begin with Docker and Windows Containers, you will probably make the following mistake: You will try to get the IP address from the image instead of the container. Ensure you get the container ID from docker ps. if you use the image name, you are asking Docker to show you the IP Address of the image. This, of course, does not make sense, because images do not have IP Addresses.

Push your images to Docker Hub

First, you must create a free Docker account here: https://cloud.docker.com/. Docker Hub lets you store and share your base images. It means that co-workers can use your base image from your repository.

You can create repositories and manually push images using the docker push command. These repositories can be either public or private. You can create one private repository for free, and unlimited public repositories.

Click on Repositories and Create. You must enter a name and an optional description. Next, choose a visibility setting for the repository.

When you need to push an image to Docker Hub, you must use three commands. The first one will tag the image:

  • Getcmd is my Docker ID
  • Lab is my private repository
  • Latest is the tag

Now run the docker images command to verify that the same image ID now exists in two different repositories.

The second command stores your credentials using the docker login command:

Now, you can push the image:

Note: You can log out of Docker Hub using docker logout command

Conclusion

In this article we discussed about the Docker commands that allow you to work with Windows containers. We also download Container images from the Docker HUB, and we have created a free Docker account to store our own images. Next, we have seen how to build our container image and create a container from the container image.

Containerization provides you with an open-ended device that can simplify your deployments in many different ways. In the next article, I will explain how to use Hyper-V Containers, how to convert Windows services to run in a Windows Container, and reveal the mysteries of Docker-compose.

I hope that this article has helped you to understand more about Windows Containers.

Load comments

About the author

Nicolas Prigent

See Profile

Nicolas Prigent works as a System Engineer, based in Switzerland with primary focus on Microsoft technologies. Nicolas is a Microsoft MVP in Cloud And Datacenter Management with 7 years experience in administering Windows Servers, Hyper-V and System Center products. He also received the "PowerShell Heroes 2016" Award. His blog can be found at www.get-cmd.com. You can follow him on Twitter @PrigentNico or you can contact him at simple-talk@get-cmd.com.

Nicolas Prigent's contributions